from waveapi import document
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import robot_abstract

from google.appengine.ext import db

import pprint

class MyWaves(db.Model):
  id = db.StringProperty(required=1)
  title = db.StringProperty(required=1)

class MyBlips(db.Model):
  id = db.StringProperty(required=1)

def OnGenericEventHandler(properties, context):
  propstr = pprint.pformat(properties)
  contstr = pprint.pformat(context)
  root_wavelet = context.GetRootWavelet()
  if root_wavelet != None:
    msg = "properies: " + propstr + "<br/>\ncontext: " + contstr
    root_wavelet.CreateBlip().GetDocument().SetText(msg)

def OnBlipCreated(props, context):
  blipId = props['blipId']
  # blipcontext.GetBlipById(blipId)

def OnBlipSubmitted(props, context):
  blipId = props['blipId']
  blip = context.GetBlipById(blipId)
  if blip != None:
    msg = None
    bliptxt = blip.GetDocument().GetText()
    if bliptxt.startswith('!'):
      creator_id = blip.GetCreator()
      cmd, text = bliptxt[1:].split(" ", 1)
      if cmd == "create":
        # make a new blip
        nw = robot_abstract.NewWave(context, context.GetRootWavelet().GetParticipants()) 
        nw.SetTitle(text)
        nwbi = nw.GetRootBlipId()
        nwblip = context.GetBlipById(nwbi)
        bidoc = nwblip.GetDocument()
        bidoc.AppendText("This is an automatically created Wave.")
        gad = document.Gadget("http://dan-dev.appspot.com/gadgets/static/poll.xml")
        bidoc.AppendElement(gad)
        msg = "New wave created."

#    if blip.IsRoot() != True:
#      msg = "Really? I don't belive : " + bliptxt
#    else:
#      msg = "New blip."
#    if bliptxt == "@create":
#      root_wave = context.GetRootWave()
#      w = root_wave.CreateWave()
      
    if msg != None:
      blip.CreateChild().GetDocument().SetText(msg)

def OnParticipantsChanged(properties, context):
  """Invoked when any participants have been added/removed."""
  added = properties['participantsAdded']
  for p in added:
    Notify(context)

def OnRobotAdded(properties, context):
  """Invoked when the robot has been added."""
  root_wavelet = context.GetRootWavelet()
  msgid = root_wavelet.GetWaveId()
  tit = root_wavelet.GetTitle()
  if tit == None:
    tit = "No Title"
  wv = MyWaves(id=msgid, title=tit)
  db.put(wv)
  # ins_id = wv.key()
  # ins_id = "FAIL"
  # msg = "I'm alive! This is " + tit + " - " + msgid + " + " + ins_id + "."
  # root_wavelet.CreateBlip().GetDocument().SetText(msg)

def Notify(context):
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("OnPerticipantsChange->Notify : " + root_wavelet.GetTitle())

if __name__ == '__main__':
  myRobot = robot.Robot('dan-dev',
      image_url='http://dan-dev.appspot.com/assets/wave/icon.jpg',
      version='3',
      profile_url='http://dan-dev.appspot.com/')
#  myRobot.RegisterHandler(events.WAVELET_PARTICIPANTS_CHANGED, OnParticipantsChanged)
#  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
#  myRobot.RegisterHandler(events.WAVELET_BLIP_CREATED, OnBlipCreated)
#  myRobot.RegisterHandler(events.WAVELET_BLIP_REMOVED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.WAVELET_PARTICIPANTS_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.WAVELET_SELF_REMOVED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.WAVELET_TIMESTAMP_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.WAVELET_TITLE_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.WAVELET_VERSION_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.BLIP_CONTRIBUTORS_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.BLIP_DELETED, OnGenericEventHandler)
  myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
#  myRobot.RegisterHandler(events.BLIP_TIMESTAMP_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.BLIP_VERSION_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.DOCUMENT_CHANGED, OnGenericEventHandler)
#  myRobot.RegisterHandler(events.FORM_BUTTON_CLICKED, OnGenericEventHandler)
  myRobot.Run()
